using System;
using System.Collections.Generic;
using System.Text;

namespace RapidHDL.Fabric
{
    public class ExternalBusBlock : Block
    {
        int iWidth;

        public ExternalBusBlock(string psName, int piWidth)
            : base(psName, "BUS")
        {
            iWidth = piWidth;
            this.CreateInput("IN", piWidth);
            this.CreateControlInput("LATCH");
            this.CreateStatusOutput("STALL");

            this.CreateOutput("OUT", piWidth);
            this.CreateControlInput("READ");
            this.CreateStatusOutput("READY");
        }

        internal override void GenerateStructure()
        {
            PipelineStage oStage = new PipelineStage(ParentComponent, "pipe_" + Name, Clock, Reset, iWidth);

            Inputs["IN"].LinkNodeVector(oStage.InputBus.Data);
            ControlInputs["LATCH"].LinkNodeVector(oStage.InputBus.DataReady);            
            StatusOutputs["STALL"].LinkNodeVector(oStage.InputBus.Stall);

            Outputs["OUT"].LinkNodeVector(oStage.OutputBus.Data);
            StatusOutputs["READY"].LinkNodeVector(oStage.OutputBus.DataReady);
            NodeVector nvRead = ParentComponent.CreateNodeVector("pipe_" + Name + "_read", 1);
            oStage.OutputBus.Stall.Join(!nvRead);
            ControlInputs["READ"].LinkNodeVector(nvRead);
            
        }
    }
}
